package com.cy.hui_jie_dan.mapper;

import com.cy.hui_jie_dan.popj.entity.FriendRequest;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 好友申请Mapper接口
 */
@Mapper
public interface FriendRequestMapper {
    
    /**
     * 根据ID查询好友申请
     * @param id 好友申请ID
     * @return 好友申请
     */
    @Select("SELECT * FROM friend_request WHERE id = #{id}")
    FriendRequest selectById(@Param("id") Long id);
    
    /**
     * 插入好友申请
     * @param friendRequest 好友申请
     * @return 影响行数
     */
    @Insert("INSERT INTO friend_request(from_user_id, to_user_id, message, status, create_time, update_time) " +
            "VALUES(#{fromUserId}, #{toUserId}, #{message}, #{status}, #{createTime}, #{updateTime})")
    int insert(FriendRequest friendRequest);
    
    /**
     * 更新好友申请
     * @param friendRequest 好友申请
     * @return 影响行数
     */
    @Update("UPDATE friend_request SET from_user_id = #{fromUserId}, to_user_id = #{toUserId}, " +
            "message = #{message}, status = #{status}, update_time = #{updateTime} " +
            "WHERE id = #{id}")
    int updateById(FriendRequest friendRequest);
    
    /**
     * 删除好友申请
     * @param id 好友申请ID
     * @return 影响行数
     */
    @Delete("DELETE FROM friend_request WHERE id = #{id}")
    int deleteById(@Param("id") Long id);
    
    /**
     * 获取某用户发送的待处理好友申请
     * @param fromUserId 发送者ID
     * @return 好友申请列表
     */
    @Select("SELECT * FROM friend_request WHERE from_user_id = #{fromUserId} AND status = 0 ORDER BY create_time DESC")
    List<FriendRequest> selectPendingRequestsByFromUserId(@Param("fromUserId") Integer fromUserId);
    
    /**
     * 获取某用户收到的待处理好友申请
     * @param toUserId 接收者ID
     * @return 好友申请列表
     */
    @Select("SELECT * FROM friend_request WHERE to_user_id = #{toUserId} AND status = 0 ORDER BY create_time DESC")
    List<FriendRequest> selectPendingRequestsByToUserId(@Param("toUserId") Integer toUserId);
    
    /**
     * 获取某用户的好友申请历史记录
     * @param userId 用户ID
     * @return 好友申请历史记录
     */
    @Select("SELECT * FROM friend_request WHERE from_user_id = #{userId} OR to_user_id = #{userId} ORDER BY create_time DESC")
    List<FriendRequest> selectRequestHistoryByUserId(@Param("userId") Integer userId);
    
    /**
     * 查询特定两个用户之间的待处理好友申请
     * @param fromUserId 发送者ID
     * @param toUserId 接收者ID
     * @return 好友申请
     */
    @Select("SELECT * FROM friend_request WHERE from_user_id = #{fromUserId} AND to_user_id = #{toUserId} AND status = 0")
    FriendRequest selectPendingRequestByUserIds(@Param("fromUserId") Integer fromUserId, @Param("toUserId") Integer toUserId);
} 